package com.jd.risk.bankorder.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.ResourceBundle;

import javax.servlet.http.HttpServletRequest;

import com.jd.risk.bankorder.entity.privilege.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wangyin.operation.beans.UploadFile;
import com.wangyin.operation.beans.UploadObject;
import com.wangyin.operation.beans.UserModel;

public class CommonUtil {
	
	static Logger logger = LoggerFactory.getLogger(CommonUtil.class);
	
	private static SimpleDateFormat formatCommon = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	private static SimpleDateFormat formatCommonForHbase = new SimpleDateFormat("yyyyMMddHH:mm:ss");
	private static SimpleDateFormat formatCommonSSS = new SimpleDateFormat("yyyyMMddHHmmssSSS");
	private static SimpleDateFormat formatCommonYMD = new SimpleDateFormat("yyyy-MM-dd");
	public static String getDateFormat(SimpleDateFormat resultFormat,SimpleDateFormat firstFormat,String dateStr){
		if(dateStr==null||"".equals(dateStr))
			return "";
		try {
			return resultFormat.format(firstFormat.parse(dateStr));
		} catch (ParseException e) {
			logger.error("",e);
			return "";
		}
	}
	
	public static SimpleDateFormat getFormatCommonYMD() {
		return formatCommonYMD;
	}

	public static void setFormatCommonYMD(SimpleDateFormat formatCommonYMD) {
		CommonUtil.formatCommonYMD = formatCommonYMD;
	}

	public static SimpleDateFormat getFormatCommonSSS() {
		return formatCommonSSS;
	}

	public static Date getDate(String dateStr,SimpleDateFormat sdf){
		Date date = null;
		try {
			date = sdf.parse(dateStr);
		} catch (Exception e) {
			logger.error("",e);
		}
		return date;
	}
	
	public static String getDateFormat(Date date,SimpleDateFormat sdf){
		try {
			return sdf.format(date);
		} catch (Exception e) {
			logger.error("",e);
			return "";
		}
	}

	public static String getDateFormatFromLong(Long time,SimpleDateFormat sdf){
		try {
			Date date = new Date(time);
			return sdf.format(date);
		} catch (Exception e) {
			logger.error("",e);
			return "";
		}
	}

	public static SimpleDateFormat getFormatCommon() {
		return formatCommon;
	}

	public static SimpleDateFormat getFormatCommonForHbase() {
		return formatCommonForHbase;
	}	
	
	public static String getUserName(HttpServletRequest request){
		String lotusUser = request.getParameter("user");
		return new UserModel(lotusUser,false).getRealName();
	}
	
	public static boolean isNotEmpty(String value){
		return value!=null&&!"".equals(value);
	}
	
	public static boolean isNotEmpty(List<?> list){
		return list!=null&&!list.isEmpty();
	}
	
//	public static <T> Object execObjectMethod(T object,String methodName,Object[] param){
//		if(object==null)
//			return null;
//		Class c = object.getClass();//得到类型
//		try {
//			Method method = c.getMethod(methodName);
//			Object result = method.invoke(object,param);
//			return result;
//		} catch (SecurityException e) {
//			logger.error("",e);
//		} catch (NoSuchMethodException e) {
//			logger.error("",e);
//		} catch (IllegalArgumentException e) {
//			logger.error("",e);
//		} catch (IllegalAccessException e) {
//			logger.error("",e);
//		} catch (InvocationTargetException e) {
//			logger.error("",e);
//		}
//		return null;
//	}
	
	public static UploadObject setImportPropertity(String uploadObjectStr,String path,boolean temp) {
		JSONObject obj = JSONObject.parseObject(uploadObjectStr);
		UploadObject uploadObject = JSON.toJavaObject(obj, UploadObject.class);
		uploadObject.setVersion("2.0");//推荐用2.0版本接口，1.0版本逐渐被废弃
		List<UploadFile> list = uploadObject.getFileList();//可能为多文件上传，所以是一个list
		for (UploadFile uploadFile : list) {
			//String path = "areaRiskLevel";
			uploadFile.setPath(path);//设置上传到NFS的相对路径(前边会自动加上"/lotus")
			String name=uploadFile.getOriginalName().substring(0,uploadFile.getOriginalName().lastIndexOf("."));
			uploadFile.setName(name+CommonUtil.getDateFormat(new Date(),CommonUtil.getFormatCommonSSS()));//设置上传到NFS的名称（不含后缀）
			uploadFile.setTemp(temp);//设为临时路径的文件会定期清理，如果需要存档的文件请不要设置true。
			uploadFile.setVersion("2.0");
		}
		return uploadObject;
	}

	public static String getPropertiesByFile(String pname , String key)
	{
		ResourceBundle resourceBundle =   ResourceBundle.getBundle(pname);
		return resourceBundle.getString(key);
	}

	public  static User getCurrentUser()
    {
        User user = new User();
        user.setBankCode("ICBC");
        return user;
    }

//	public static String getMerchantName(String merchantNo,MerchantBaseInfoWyMapper merchantBaseInfoWyMapper){
//		if(!CommonUtil.isNotEmpty(merchantNo)){
//			return "";
//		}
//		MerchantBaseInfoWy mbiw = new MerchantBaseInfoWy();
//		try{
//			mbiw.setLimit(1);
//			mbiw.setStart(0);
//			mbiw.setMerchantNumber(Long.valueOf(merchantNo));
//		}catch(Exception e){
//			logger.error("",e);
//		}
//		List<MerchantBaseInfoWy> queryMerchantByMerchantBaseInfoWy = merchantBaseInfoWyMapper.queryMerchantByMerchantBaseInfoWy(mbiw);
//		if(isNotEmpty(queryMerchantByMerchantBaseInfoWy)){
//			return queryMerchantByMerchantBaseInfoWy.get(0).getFirstParty();
//		}
//		return "";
//	}
}
